# 多项式回归 采用R公式  6次多项式的数据
# 供参考
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.formula.api as smf
import statsmodels.api as sm
from sklearn.metrics import mean_squared_error

x = np.linspace(-1, 1, 101).reshape(-1, 1)
num_cofffs = 6
coeffs = [1, 2, 3, 4, 5, 6]
y_true = 0
for i in range(num_cofffs):
    y_true += coeffs[i] * np.power(x, i)

print(y_true.shape)
print(x.shape)

y = y_true + np.random.randn(*x.shape) * 1.5
# plt.figure(1)
plt.scatter(x, y)
# plt.show()
# plt.pause(1)


data = {'x': x, 'y': y}
model = smf.ols(formula='y ~ 1 + x + I(x**2) + I(x**3) + I(x**4)+ I(x**5) + I(x**6)', data=data).fit()

pred = model.fittedvalues
rmse = np.sqrt(mean_squared_error(y, pred))
print(rmse)
model.summary()
# w = model.params


plt.figure(1)
plt.plot(x, pred, 'r', label='line 1')
plt.legend()

###############################################对比
x2 = x ** 2
x3 = x ** 3
x4 = x ** 4
x5 = x ** 5
x6 = x ** 6
XX = np.concatenate((x, x2, x3, x4, x5, x6), axis=1)
XX = sm.add_constant(XX)
model = sm.OLS(y, XX).fit()
pred = model.fittedvalues
print(model.summary())
##############################################
plt.figure(1)
plt.plot(x, pred, 'r', 'predict')



plt.pause(0)
